﻿using _1_Interfaces.Interfaces.Cjw;
using _1_Interfaces.Models.Dtos.Cjw;
using _1_Interfaces.Models.Tables.Cjw;
using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _2_Services.Services.Cjw
{
    /// <summary>
    /// 统计分析服务
    /// </summary>
    public class StatisticsService : IStatisticsService
    {
        SqlSugarClient _db = CjwDbContext.GetInstance();
        private IMapper _mapper;

        public StatisticsService(IMapper mapper)
        {
            _mapper = mapper;
        }

        /// <summary>
        /// 统计分析
        /// </summary>
        /// <param name="departmentId">科室id</param>
        /// <returns></returns>
        public StatisticsDto GetStatistics(int departmentId)
        {
            DateTime dateTime = DateTime.Now.Date;
            StatisticsDto statisticsDto = new StatisticsDto();
            statisticsDto.DepartmentId = departmentId;
            //获取10天内的所有出入科记录
            List<InOutList> inOutLists = _db.Queryable<InOutList>()
                .Where(x => x.DepartmentId == departmentId)
                .Where(x => Convert.ToDateTime(x.Time) >= dateTime.AddDays(-10))
                .ToList();
            //获取今天的入科的人数
            statisticsDto.InDepartmentTimeToday = inOutLists
                .Where(x => Convert.ToDateTime(x.Time) >= dateTime)
                .Where(x => x.InOut == 1)
                .Count();
            //获取今天的出科的人数
            statisticsDto.LeaveDepartmentTimeToday = inOutLists
                .Where(x => Convert.ToDateTime(x.Time) >= dateTime)
                .Where(x => x.InOut == 2)
                .Count();
            //获取昨天的入科的人数
            statisticsDto.InDepartmentTimeYesterday = inOutLists
                .Where(x => Convert.ToDateTime(x.Time) >= dateTime.AddDays(-1) && Convert.ToDateTime(x.Time) < dateTime)
                .Where(x => x.InOut == 1)
                .Count();
            //获取昨天的出科的人数
            statisticsDto.LeaveDepartmentTimeYesterday = inOutLists
                .Where(x => Convert.ToDateTime(x.Time) >= dateTime.AddDays(-1) && Convert.ToDateTime(x.Time) < dateTime)
                .Where(x => x.InOut == 2)
                .Count();
            //获取10天的出入科
            for (int i = -9; i < 1; i++)
            {
                DateTime time = dateTime.AddDays(i);
                int m = inOutLists
                    .Where(x => Convert.ToDateTime(x.Time) >= time && Convert.ToDateTime(x.Time) < time.AddDays(1))
                    .Where(x => x.InOut == 1)
                    .Count();
                int n = inOutLists
                    .Where(x => Convert.ToDateTime(x.Time) >= time && Convert.ToDateTime(x.Time) < time.AddDays(1))
                    .Where(x => x.InOut == 2)
                    .Count();
                double inOut = n == 0 ? 0 : Convert.ToDouble(m) / Convert.ToDouble(n);

                statisticsDto.Times.Add(time.ToString("MM/dd"));
                statisticsDto.Ins.Add(m);
                statisticsDto.Outs.Add(n);
                statisticsDto.InOuts.Add(Math.Round(inOut, 2, MidpointRounding.AwayFromZero));
            }

            //获取所有病床
            List<SickBedList> sickBedLists = _db.Queryable<SickBedList>()
                .ToList();
            //科室病床总数量
            statisticsDto.SickBedTotal = sickBedLists
                .Where(x => x.DepartmentId == departmentId)
                .Count();
            //科室里躺在病床上的总人数
            statisticsDto.InSickBedNumber = sickBedLists
                .Where(x => x.DepartmentId == departmentId)
                .Where(x => x.SickBedState == 2)
                .Count();
            //空闲的病床数量
            statisticsDto.SickBedNotInUse = sickBedLists
                .Where(x => x.DepartmentId == departmentId)
                .Where(x => x.SickBedState == 1)
                .Count();
            //科室里坏的病床数量
            statisticsDto.SickBedDamage = sickBedLists
                .Where(x => x.DepartmentId == departmentId)
                .Where(x => x.SickBedState == 0)
                .Count();
            
            //获取科室列表
            List<DepartmentList> departmentLists = _db.Queryable<DepartmentList>()
                .ToList();
            //获取科室id列表
            List<int> departmentIds = departmentLists
                .Select(x => x.DepartmentId)
                .ToList();
            //总共科室的病床数量统计
            foreach (int id in departmentIds)
            {
                SickBedDto sickBedDto = new SickBedDto();
                sickBedDto.Id = id;
                sickBedDto.Name = departmentLists
                    .Where(x => x.DepartmentId == id)
                    .Select(x => x.DepartmentName)
                    .First();
                sickBedDto.Value = sickBedLists
                    .Where(x => x.DepartmentId == id)
                    .Count();
                statisticsDto.SickBedsInAll.Add(sickBedDto);
            }

            // 科室下启用中，空闲，坏的病床数量
            for (int j = 0; j < 3; j++)
            {
                SickBedDto sickBedDto = new SickBedDto();
                sickBedDto.Id = departmentId;
                if (j == 0)
                {
                    sickBedDto.Name = "故障";
                }
                else if (j == 1)
                {
                    sickBedDto.Name = "空闲";
                }
                else
                {
                    sickBedDto.Name = "使用中";
                }
                sickBedDto.Value = sickBedLists
                    .Where(x => x.DepartmentId == departmentId)
                    .Where(x=>x.SickBedState==j)
                    .Count();
                statisticsDto.SickBedsInDepartment.Add(sickBedDto);
            }

            return statisticsDto;
        }
    }
}
